package com.mp.generator.run;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: GlobalConfigs.java
 * @Program: generator
 * @Date: 2022/9/23 上午8:57
 * @Author: Zhaopeiqiang
 * @Description:
 */
public class GlobalConfigs {
    /**
     * 数据库地址
     */
    private static final String DB_URL = "jdbc:mysql://172.160.3.159:3306/remind?useUnicode=true&characterEncoding=utf-8";
    /**
     * db 账号
     */
    private static final String USERNAME = "root";
    /**
     * db 密码
     */
    private static final String PASSWORD = "root@1";
    /**
     * db 驱动
     */
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    /**
     * 存放路径，包路径
     */
    private static final String PACKAGE = "com.mp.generator.gen";
    /**
     * 需要生成的数据库表名，数组，可多个
     */
    private static final String[] TABLE_NAME = {"t_plan"};
    /**
     * 需要过滤的表前缀
     */
    private static final String[] TABLE_PREFIX = {"t_", "sys_"};

    public static void main(String[] args) {
        GlobalConfig config = new GlobalConfig();
        String path = System.getProperty("user.dir");
        config.setActiveRecord(true)
                .setAuthor("Zhaopeiqiang")
                .setOutputDir(path + "/src/main/java/")
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setFileOverride(false)
                // id自动填充类型
                .setIdType(IdType.ASSIGN_ID)
                //设置是否覆盖原始生成的文件
                .setFileOverride(true);

        //****************************** 配置 ******************************************************
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(DB_URL)
                .setUsername(USERNAME)
                .setPassword(PASSWORD)
                .setDriverName(DRIVER_NAME)
                .setTypeConvert(new MySqlTypeConvert() {
                    @Override
                    public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                        System.out.println("转换类型：" + fieldType);
                        //tinyint转换成String
                        if (fieldType.toLowerCase().contains("tinyint")) {
                            return DbColumnType.STRING;
                        }
                        if (fieldType.toLowerCase().contains("datetime")) {
                            return DbColumnType.DATE;
                        }
                        return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
                    }
                });

        //****************************** Policy configuration *************************************
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));
        tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));
        tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));
        tableFillList.add(new TableFill("gmt_create", FieldFill.INSERT));
        tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));
        tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));
        tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true).setEntityLombokModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setTableFillList(tableFillList)
                .setTablePrefix(TABLE_PREFIX)
                .setInclude(TABLE_NAME);

        //是否启用Rest风格
        strategyConfig.setRestControllerStyle(true);
        //设置乐观锁字段名
        // strategyConfig.setVersionFieldName("version"); 
        //设置逻辑删除字段名
        strategyConfig.setLogicDeleteFieldName("deleted");

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(new PackageConfig()
                        .setParent(PACKAGE)
                        .setController("controller")
                        .setEntity("domain")
                        .setMapper("dao")
                        .setXml("dao"))
                .setTemplate(new TemplateConfig().setServiceImpl("templates/serviceImpl.java.vm"))
                .setTemplate(new TemplateConfig().setMapper("templates/mapper.java.vm"))
                .setTemplate(new TemplateConfig().setEntity("templates/entity.java.vm"))
                .setTemplate(new TemplateConfig().setXml("templates/mapper.xml.vm"))
                .execute();
    }
}
